﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using WeChatDemo.Models;
using WeChatDemo.DataProvider;

namespace WeChatDemo.Controllers
{
    public class ThumbupController : ApiController
    {
        [HttpGet]
        public IHttpActionResult Get()
        {
            IEnumerable<Thumbup> _list = null;
            _list = DAOThumbup.GetAllList();

            if (_list.Count<Thumbup>() == 0)
            {
                return NotFound();
            }
            else
            {
                return Ok(_list);
            }
        }

        [HttpPost]
        public IHttpActionResult Post([FromBody] Thumbup model)
        {
            //ajax客户端发送{"openid":"oRMP5shI6hdqW7Fs7quu9yuU3HKg","nickname":"吴淞人", "articleid":"409069ed-54e6-4324-97d1-5b96f553b0ee"}
            Thumbup _model = new Thumbup();
            model.OpenID = model.OpenID;
            model.NickName = model.NickName;
            model.ArticleID = model.ArticleID;

            if (DAOThumbup.Add(model))
            {
                HttpResponseMessage _message = new HttpResponseMessage();
                _message.StatusCode = HttpStatusCode.Created;
                _message.Content = new StringContent("success");
                return ResponseMessage(_message);
            }
            else
            {
                return InternalServerError();
            }
        }

        [HttpDelete]
        public IHttpActionResult Delete(int id)
        {
            if (DAOThumbup.Delete(id))
            {
                HttpResponseMessage _message = new HttpResponseMessage();
                _message.StatusCode = HttpStatusCode.NoContent;
                _message.Content = new StringContent("success");
                return ResponseMessage(_message);
            }
            else
            {
                return InternalServerError();
            }
        }
    }
}
